function initArtCateList() {
  $.ajax({
    type: "GET",
    url: "/my/article/cates",
    success: (res) => {
      console.log(res);
      let htmltsl = template("tpl_cate", res);
      $("tbody").html(htmltsl);
    },
  });
}
initArtCateList();

let index = null;
let layer = layui.layer;
let form = layui.form;


$("#btnAddCate").on("click", function () {
  index = layer.open({
    type: 1,
    area: ["500px", "250px"],
    title: "添加文章分类",
    content: $("#dialog-add").html(),
  });
});

$("body").on("submit", "#form-add", function (e) {
  e.preventDefault();
  $.ajax({
    type: "POST",
    url: "/my/article/addcates",
    data: $(this).serialize(),
    success: (res) => {
      if (res.status !== 0) return layer.msg(res.message);
      initArtCateList();
      layer.msg(res.message);
      layer.close(index);
    },
  });
});

$("tbody").on("click", ".btn-delete", function () {
  let id = $(this).attr("data-id");
  layer.confirm("确认删除?", { icon: 3, title: "提示" }, function (index) {
    $.ajax({
      method: "GET",
      url: "/my/article/deletecate/" + id,
      success: (res) => {
        if (res.status !== 0) return layer.msg(res.message);
        layer.msg(res.message);
        layer.close(index);
        initArtCateList();
      },
    });
  });
});

let addindex = null;
$("tbody").on("click", ".btn-edit", function () {
  addindex = layer.open({
    type: 1,
    area: ["500px", "250px"],
    title: "修改文章分类",
    content: $("#dialog-edit").html(),
  });
  let id = $(this).attr("data-id");
  $.ajax({
    method: "GET",
    url: "/my/article/cates/" + id,
    success: (res) => {
      form.val("form-edit", res.data);
    },
  });
});

$('body').on('submit','#form-edit',function(e) {
  e.preventDefault()
  $.ajax({
    method:'POST',
    url:'/my/article/updatecate',
    data: $(this).serialize(),
    success: res => {
      if (res.status !== 0) return layer.msg(res.message)
      layer.msg(res.message)
      layer.close(addindex);
      initArtCateList();
    }
  })
})
